﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BRS.BidModule
{
    public partial class BidderListfrm : Form
    {
        Entities brs = new Entities();
        int bidderid = 0;
        static string Dir = ConfigurationManager.AppSettings["ip"];
        static string ServerUser = ConfigurationManager.AppSettings["user"];
        static string ServerPassword = ConfigurationManager.AppSettings["password"];
        string filepath = ConfigurationManager.AppSettings["filepath"];
        //public BidderListfrm()
        //{
        //    InitializeComponent();
        //}
        public BidderListfrm(int data)
        {
            InitializeComponent();
            bidderid = data;
            this.dataGridView1.AutoGenerateColumns = false;
            Binds(data);
        }
        public void Binds(int data)
        {
            tb_Bidder bid = brs.tb_Bidder.Find(data);
          
            var bidder = brs.BBInfo.Where(p => p.BidderId == bid.ISID).Select(p => p.BidderInfoId).ToList();
            var cc = brs.BidderInfo.Where(p =>bidder.Contains(p.Id) ).OrderByDescending(p=>p.CreateDate).ToList();
            var list = new List <BidderInfo>();

             var p1 = cc.Where(p => p.BidderType == "法定代表人").OrderByDescending(p => p.CreateDate).FirstOrDefault(); 
             if (p1!=null)
             {
                 list.Add(p1);
             }
             var p2 = cc.Where(p => p.BidderType == "企业负责人").OrderByDescending(p => p.CreateDate).FirstOrDefault();
             if (p2 != null)
             {
                 list.Add(p2);
             } 
            var p3 = cc.Where(p => p.BidderType == "企业技术负责人").OrderByDescending(p => p.CreateDate).FirstOrDefault();
             if (p3 != null)
             {
                 list.Add(p3);
             } 
            var p4 = cc.Where(p => p.BidderType == "项目负责人").OrderByDescending(p => p.CreateDate).FirstOrDefault();
             if (p4 != null)
             {
                 list.Add(p4);
             }
            //var pros = brs.tb_Project.ToList();
            dataGridView1.DataSource = list;
            //label2.Text=

            //LiftPicture.ImageLocation = @bid.FilePath + bid.Company + "/现场" + bid.CompanyCode + "_" + time + ".jpg";
            //IDPitcture.ImageLocation = @bid.FilePath + bid.Company + "/证件" + bid.CompanyCode + "_" + time + ".jpg";
            //Scanpicture.ImageLocation = @bid.FilePath + bid.Company + "/扫描" + bid.CompanyCode + "_" + time + ".jpg";
            ////MessageBox.Show(IDPitcture.ImageLocation.ToString());

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string data = this.dataGridView1.CurrentRow.Cells["Id"].Value.ToString();
            int GetData = int.Parse(data);
            frmBidderDetail bidder = new frmBidderDetail(GetData);
            bidder.ShowDialog();

        }

        private void button1_Click(object sender, EventArgs e)
        { 
            FtpWeb ftpweb = new FtpWeb(Dir, "投标人信息", ServerUser, ServerPassword);
            //FtpWeb ftpweb=new FtpWeb();
           
            tb_Bidder bid = brs.tb_Bidder.Find(bidderid); 
            ftpweb.Dir("投标人信息/" + bid.Company);
            string time = bid.CreateDate.ToString(
                         "yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
            string scanpath = filepath + "/投标人信息/" + bid.Company + "/扫描" + "_" + time + ".jpg";
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);//本地创建目录
            }
            if (!Directory.Exists(filepath+"/投标人信息/"))
            {
                Directory.CreateDirectory(filepath + "/投标人信息/");//本地创建目录
            }
            if (!Directory.Exists(filepath + "/投标人信息/" + bid.Company))
            {
                Directory.CreateDirectory(filepath + "/投标人信息/" + bid.Company);//本地创建目录
            }
            if (!File.Exists(scanpath))   //判断文件是否存在
            {
                ftpweb.Download(filepath + "/投标人信息/" + bid.Company, "扫描" + "_" + time + ".jpg");
            }
            ScanPaperfrm scan = new ScanPaperfrm(scanpath);
            scan.ShowDialog();

        }
    }
}
